/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InsertObjectDialog.java
 *
 * Created on Oct 2, 2010, 11:41:25 AM
 */

package net.worlds.component;

import net.worlds.ui.UI;
import java.awt.event.KeyEvent;
import java.text.Collator;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import net.worlds.Main;
import net.worlds.ObjectList;
import net.worlds.Util;
import net.worlds.World;
import net.worlds.object.Box;
import net.worlds.object.FixedRotation;
import net.worlds.object.GameObject;
import net.worlds.object.NPC;
import net.worlds.object.Part;
import net.worlds.object.Prop;
import net.worlds.ui.ObjectListCellRenderer;
import org.jbox2d.common.Vec2D;

/**
 *
 * @author Jacob Morgan
 */
public class InsertObjectDialog extends javax.swing.JFrame {
    private DefaultListModel model = new DefaultListModel();
    public InsertObjectDialog() {
        initComponents();
        setLocationRelativeTo(Main.getApplet());
        try
        {
            setIconImages(Util.icons);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        objectList.setCellRenderer(new ObjectListCellRenderer());
        ArrayList<Class> classes = ObjectList.getAllClasses();

        objectList.setModel(model);
        String[] a = new String[classes.size()];
        int i = 0;
        for(Class c : classes)
        {
            if(ObjectList.getDef(c.getSimpleName()).visible)
            {
                a[i] = (String)c.getSimpleName();
                i++;
            }
        }
        Util.sortArray(Collator.getInstance(), a);
        for(int j = 0; j < i; j++)
        {
            model.addElement(a[j]);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        objectList = new javax.swing.JList();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        infoButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Insert Object");

        objectList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        objectList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                objectListMouseClicked(evt);
            }
        });
        objectList.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                objectListKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(objectList);

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        infoButton.setText("More Info...");
        infoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                infoButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(infoButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(infoButton)
                    .addComponent(cancelButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        final String t = (String) model.getElementAt(objectList.getSelectionModel().getMaxSelectionIndex());
            Main.runInMainThread(new Runnable()
            {
                public void run()
                {
                    try
                    {
                        World world = Main.getGame().getCurrentWorld();
                        GameObject obj = world.createObject(t);
                        if(UI.getNumberSelected() == 1)
                            obj.setParent(UI.getSelected().get(0));
                        else
                            obj.setParent(world.getService("World"));
                        if(obj instanceof Part)
                        {
                            obj.setProp("Position", world.getService("Camera").getVec2DProp("Position"), true);
                        }
                        else if(obj instanceof NPC)
                        {
                            Box b = (Box) world.createObject("Box");
                            b.setProp("Size", new Vec2D(2.5f, 10.5f), true);
                            b.setProp("Name", "Body", true);
                            b.setProp("Transparency", 1.0f, true);
                            b.setProp("Position", world.getService("Camera").getProp("Position"), true);
                            FixedRotation c = (FixedRotation) world.createObject("FixedRotation");
                            c.setProp("Parent", b, true);
                            b.setParent(obj);
                            UI.moveToFront((Prop)obj);
                        }
                    }
                    catch(Exception ex)
                    {
                        Main.error(ex);
                    }
                }
            });
        dispose();
    }//GEN-LAST:event_okButtonActionPerformed

    private void objectListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_objectListMouseClicked
        if(objectList.getSelectedIndex() != -1)
        {
            if(evt.getClickCount() == 2)
                okButtonActionPerformed(null);
        }
    }//GEN-LAST:event_objectListMouseClicked

    private void objectListKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_objectListKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER && objectList.getSelectedIndex() != -1)
        {
            okButtonActionPerformed(null);
        }
    }//GEN-LAST:event_objectListKeyPressed

    private void infoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_infoButtonActionPerformed
        int idx = objectList.getSelectionModel().getMaxSelectionIndex();
        String object;
        if(idx == -1)
            object = "Object_List";
        else
            object = (String) model.getElementAt(idx);
        Util.wikiLookup(object);
    }//GEN-LAST:event_infoButtonActionPerformed

    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton infoButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList objectList;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables

}
